﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="js/jquery.min.js" type="text/javascript"></script>
</head>
<body>
  <input type="text" id="goodscode" name="goodscode" onkeydown="keyfilter();"  style="width:240px;height:30px">
  <span id="scan"></span>
  <table>
   <tr id="head" style="color:#999" height="40px;"><td width="300px" >商品名称</td><td width="100px">单价</td><td width="100px" >数量</td><td width="100px">总价</td></tr>
 </table>
 <table>
   <tr  style="font-size:30px;color:red"><td width="500px" align="right">合计:</td><td id="all"></td><td><input type="button" value="同步" onclick="syn()"></td></tr>
 </table>
</body>
<script>
 function keyfilter()
{
  var keycode = event.keyCode;
  if(keycode == 13)
  {
    var scan=$("#goodscode").val();
    $("#scan").html(scan);
    $("#goodscode").val('');
    if($("#"+scan).length>0)
	 {
	  $("#"+scan).find('.num').html($("#"+scan).find('.num').html()*1+1);
	  $("#"+scan).find('.tprice').html($("#"+scan).find('.tprice').html()*1+$("#"+scan).find('.price').html()*1);
	  settotal();
	  return;
	 }
     var url='<%=request.getContextPath()%>/goods/scan.do?scan='+scan;
	 $.get(url, function(result){	
	   if(result)
	   {
	   var goodsinfo=eval('('+result+')');
	   var trhtml='<tr style="font-size:24px" id='+scan+' rid='+goodsinfo.id+' class=ginfo height="50px;"><td>'+goodsinfo.goodsname+'</td><td class=price>'+goodsinfo.price+'</td><td class=num>1</td><td class=tprice>'+goodsinfo.price+'</td></tr>'
       $("#head").after(trhtml);
       settotal();
	   }else
	   {
	     $("#scan").html(scan+' <span style="color:red">该商品条码不存在！</span>');
	   }
	  }); 
  }

}

function settotal()
{
  var totalprice=0;
 $(".tprice").each(function()
  {
    totalprice=totalprice*1+$(this).html()*1;   
  });
   $("#all").html(totalprice);

}

function syn()
{
  var goodsinfo="";
  $(".ginfo").each(function()
  {
    var id=$(this).attr("rid");
    var num=$(this).find(".num").html();
    if(goodsinfo=='')
    {
    goodsinfo=id+"_"+num;
    }
    else
    {
     goodsinfo=goodsinfo+","+id+"_"+num;
    }
  });
  
   if(goodsinfo!='')
   {
     var url='<%=request.getContextPath()%>/goods/scandel.do?goodsinfos='+goodsinfo;
	 $.get(url, function(result){	
	    alert('扣减库存成功!');
	    $(".ginfo").remove();
	    $("#all").html('');
	    $("#scan").html('');
	    $("#goodscode").focus()
	  }); 
	 }
}

$("#goodscode").focus();

</script>
</html>